/*
 * The MIT License
 * Copyright (c) 2012 kc4.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.kc4.sso.auth.section.provider;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.kc4.sso.auth.SSOContextParameters;
import org.kc4.sso.auth.SSOProviderInitParameters;
import org.kc4.sso.auth.section.InterruptedSectionException;
import org.kc4.sso.auth.section.ModelSection;
import org.kc4.sso.filter.SSOProviderFilter.EnduserSignonSequence;
import org.kc4.util.NamedValuesUriBuilder;

/**
 * @author yoichi kikuchi
 *
 */
public class FailedSignonOnInvalidSessionSection extends ModelSection {

	/**
	 * インスタンスを生成します。
	 *
	 * @param request リクエスト。
	 * @param response レスポンス。
	 */
	public FailedSignonOnInvalidSessionSection(HttpServletRequest request, HttpServletResponse response) {

		super(request, response);
	}

	@Override
	public void execute() throws InterruptedSectionException {

		HttpSession session = this.getRequest().getSession();

		/*
		 * SSOセッション無効
		 */
		String contextUrlByUrl = this.getRequest().getParameter(SSOContextParameters.getInstance().getNamingOfContextUrl());
		String callbackByUrl = this.getRequest().getParameter(SSOContextParameters.getInstance().getNamingOfCallback());
		String publickeyByUrl = this.getRequest().getParameter(SSOContextParameters.getInstance().getNamingOfPublickey());
		String signonToken = this.getRequest().getParameter(SSOContextParameters.getInstance().getNamingOfSignonToken());

		EnduserSignonSequence seq = (EnduserSignonSequence) session.getAttribute(signonToken);

		/*
		 * SSOセッション無効 > サインオンシーケンス無効、または サインオン失敗
		 *
		 * => リダイレクト : [サインオン]
		 * => パラメーター : [サインオントークン]
		 */
		if (seq == null) {
			seq = new EnduserSignonSequence();
			seq.setCallback(callbackByUrl);
			seq.setContextUrl(contextUrlByUrl);
			seq.setPublickey(publickeyByUrl);
		}
		session.removeAttribute(signonToken);
		signonToken = this.newSignonToken(session);
		NamedValuesUriBuilder b = new NamedValuesUriBuilder(this.getRequest().getContextPath()
			+ SSOProviderInitParameters.getInstance().getSignonPage());
		b.append(SSOContextParameters.getInstance().getNamingOfSignonToken(), signonToken);
		session.setAttribute(signonToken, seq);

		this.sendRedirect(b.toString());
		throw new InterruptedSectionException(false);
	}

	/**
	 * サインオントークンを生成します。
	 *
	 * @param session セッション。
	 * @return サインオントークン。
	 */
	protected String newSignonToken(HttpSession session) {

		String token = null;

		while (token == null || session.getAttribute(token) != null) {

			token = UUID.randomUUID().toString().replaceAll("-", "");
		}

		return token;
	}
}
